package com.levenx.error;

import com.levenx.config.LocaleMessage;
import com.levenx.constant.ResultStatus;
import com.levenx.error.exceptions.ObjectTransformException;
import com.levenx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionResolver {

    @Autowired
    private LocaleMessage localeMessage;

    @ExceptionHandler(NullPointerException.class)
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public Result handleNullPointerException(NullPointerException ex) {
        return Result.newResult()
                .ofStatus(ResultStatus.NULL_POINTER,"NULL_POINTER");
    }


    @ExceptionHandler(ObjectTransformException.class)
    @ResponseBody
    public Result handleObjectTransformException(ObjectTransformException ex) {
        return Result.newResult()
                .ofStatus(ResultStatus.OBJECT_TRANSFORM_EROR,localeMessage.getMessage("OBJECT_TRANSFORM_EROR"));
    }
}
